<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Lang extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('lang')
            ->setDescription('the lang command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('lang');
        $lang = [
            'en' => require './public/en.php'
        ];
        $files = $this->scanDirectory('./view/admin/cn');
        $pathdir = './view/admin/';
        foreach ($lang AS $index => $item) {
            foreach ($files AS $key => $value) {
                $path = str_replace(['\\', './view/admin/cn/'], ['/', ''], $value);
                is_dir($pathdir.$index) || mkdir($pathdir.$index,0777);
                if (strpos($path,'/')){
                    $filesdir = explode('/',$path);
                    $pathdir_temp = $pathdir.'/'.$index;
                    foreach ($filesdir AS $keys => $values){
                        if ($keys == count($filesdir) - 1)break;
                        $pathdir_temp .= '/'.$values;
                        is_dir($pathdir_temp) || mkdir($pathdir_temp,0777);
                    }
                }
                $html = file_get_contents($value);
                $html = str_replace(array_keys($item),array_values($item),$html);
                file_put_contents($pathdir.$index.'/'.$path,$html);
            }
        }
        dd($files);
    }

    private function scanDirectory($dir, &$files = []){
        $items = scandir($dir);
        foreach ($items as $item) {
            if ($item === '.' || $item === '..') continue;
            $path = $dir . DIRECTORY_SEPARATOR . $item;
            if (is_dir($path)) {
                $this->scanDirectory($path, $files); // 递归遍历子目录
            } else {
                $files[] = $path; // 记录文件路径
            }
        }
        return $files;
    }
}
